package org.jeecg.modules.controller;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.util.JwtUtil;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.dto.WithdrawalInformationDto;
import org.jeecg.modules.entity.CuUserDetails;
import org.jeecg.modules.entity.CuWithdrawalRecord;
import org.jeecg.modules.service.ICuUserDetailsService;
import org.jeecg.modules.service.ICuWithdrawalRecordService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;

/**
 * @Description: 提现记录
 * @Author: jeecg-boot
 * @Date: 2024-08-10
 * @Version: V1.0
 */
@Tag(name = "提现记录")
@RestController
@RequestMapping("/cuckoo/cuWithdrawalRecord")
@Slf4j
public class CuWithdrawalRecordController extends JeecgController<CuWithdrawalRecord, ICuWithdrawalRecordService> {

    @Autowired
    private ICuWithdrawalRecordService cuWithdrawalRecordService;

    @Autowired
    private ICuUserDetailsService userService;

    /**
     * 分页列表查询
     *
     * @param cuWithdrawalRecord
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    //@AutoLog(value = "提现记录-分页列表查询")
    @Operation(summary = "提现记录-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<CuWithdrawalRecord>> queryPageList(CuWithdrawalRecord cuWithdrawalRecord,
                                                           @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                           @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                           HttpServletRequest req) {
        QueryWrapper<CuWithdrawalRecord> queryWrapper = QueryGenerator.initQueryWrapper(cuWithdrawalRecord, req.getParameterMap());
        Page<CuWithdrawalRecord> page = new Page<CuWithdrawalRecord>(pageNo, pageSize);
        IPage<CuWithdrawalRecord> pageList = cuWithdrawalRecordService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param cuWithdrawalRecord
     * @return
     */
    @AutoLog(value = "提现记录-添加")
    @Operation(summary = "提现记录-添加")
    @RequiresPermissions("cuckoo:cu_withdrawal_record:add")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody CuWithdrawalRecord cuWithdrawalRecord) {
        cuWithdrawalRecordService.save(cuWithdrawalRecord);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param cuWithdrawalRecord
     * @return
     */
    @AutoLog(value = "提现记录-编辑")
    @Operation(summary = "提现记录-编辑")
    @RequiresPermissions("cuckoo:cu_withdrawal_record:edit")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody CuWithdrawalRecord cuWithdrawalRecord) {
        cuWithdrawalRecordService.updateById(cuWithdrawalRecord);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "提现记录-通过id删除")
    @Operation(summary = "提现记录-通过id删除")
    @RequiresPermissions("cuckoo:cu_withdrawal_record:delete")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
        cuWithdrawalRecordService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "提现记录-批量删除")
    @Operation(summary = "提现记录-批量删除")
    @RequiresPermissions("cuckoo:cu_withdrawal_record:deleteBatch")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.cuWithdrawalRecordService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "提现记录-通过id查询")
    @Operation(summary = "提现记录-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<CuWithdrawalRecord> queryById(@RequestParam(name = "id", required = true) String id) {
        CuWithdrawalRecord cuWithdrawalRecord = cuWithdrawalRecordService.getById(id);
        if (cuWithdrawalRecord == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(cuWithdrawalRecord);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param cuWithdrawalRecord
     */
    @RequiresPermissions("cuckoo:cu_withdrawal_record:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, CuWithdrawalRecord cuWithdrawalRecord) {
        return super.exportXls(request, cuWithdrawalRecord, CuWithdrawalRecord.class, "提现记录");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequiresPermissions("cuckoo:cu_withdrawal_record:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, CuWithdrawalRecord.class);
    }

    /**
     * 插入提现记录
     */
    @AutoLog(value = "提现记录-提现记录")
    @Operation(summary = "提现记录-提现记录")
    @RequiresPermissions("cuckoo:cu_withdrawal_record:insertWithdrawalRecord")
    @PostMapping(value = "/insertWithdrawalRecord")
    public Result<String> insertWithdrawalRecord(@RequestBody WithdrawalInformationDto informationDto, HttpServletRequest req) {
        String username = JwtUtil.getUserNameByToken(req);
        CuUserDetails user = userService.getByUsername(username);

        if (StringUtils.isEmpty(informationDto.getAccount())) {
            return Result.error("请输入提现账号");
        }
        if (StringUtils.isEmpty(informationDto.getName())) {
            return Result.error("请输入提现姓名");
        }
        if (StringUtils.isEmpty(informationDto.getPhone())) {
            return Result.error("请输入提现手机号");
        }
        if (StringUtils.isEmpty(informationDto.getType())) {
            return Result.error("请输入提现类型");
        }
        if (StringUtils.isEmpty(informationDto.getBalance())) {
            return Result.error("请输入提现金额");
        }
        BigDecimal balance = new BigDecimal(informationDto.getBalance());
        if (balance.compareTo(new BigDecimal(0)) < 0) {
            return Result.error("提现金额必须大于0");
        }
        informationDto.setUserId(user.getId());

        boolean isSuccess = cuWithdrawalRecordService.insertWithdrawalRecord(informationDto, user);
        if (!isSuccess) {
            return Result.error("提现失败");
        }
        return Result.OK("提现成功");
    }

}
